package com.geek.novel.ui.switch

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import androidx.appcompat.widget.SwitchCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.geek.novel.R
import com.geek.novel.utils.UiUtils
import skin.support.content.res.SkinCompatResources
import skin.support.widget.SkinCompatHelper
import skin.support.widget.SkinCompatSupportable

class SwitchCompat : androidx.appcompat.widget.SwitchCompat, SkinCompatSupportable {

    constructor(context: Context): super(context){
        this.init(context, null, 0)
    }

    constructor(context: Context, attrs: AttributeSet): super(context, attrs){
        this.init(context, attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr){
        this.init(context, attrs, defStyleAttr)
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int){
        this.initAttrs(attrs, defStyleAttr)
        this.applyTopBarBackgroundColor()

        //获取颜色
        val switchColor = context.getColor(R.color.book_dialog_menu_item_switch)
        val switchColorGrey = context.getColor(R.color.book_dialog_menu_item_switch_grey)
        setSwitchColor(switchColor, switchColorGrey)
    }


    fun setSwitchColor(thumbColor: Int, thumbColorGrey: Int) {
        background = null
        // set the thumb color
        DrawableCompat.setTintList(
            thumbDrawable, ColorStateList(
                arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()), intArrayOf(
                    thumbColor,
                    0xfff1f1f1.toInt()
                )
            )
        )
        // set the track color
        DrawableCompat.setTintList(
            trackDrawable, ColorStateList(
                arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()), intArrayOf(
                    thumbColorGrey,
                    0x4d2f2f2f
                )
            )
        )
    }



    /**控件背景的资源id*/
    private var viewTextColorResId: Int = SkinCompatHelper.INVALID_ID

    /**初始化属性*/
    private fun initAttrs(attrs: AttributeSet?, defStyleAttr: Int=0) {
        if(attrs != null){
            //获取属性
            val array = context.obtainStyledAttributes(attrs, R.styleable.SwitchCompat, defStyleAttr, 0)
            viewTextColorResId = array.getResourceId(R.styleable.SwitchCompat_switchTextColor, SkinCompatHelper.INVALID_ID)
            array.recycle()
        }
    }

    //换肤处理
    private fun applyTopBarBackgroundColor() {
        viewTextColorResId = SkinCompatHelper.checkResourceId(viewTextColorResId)
        if (viewTextColorResId != SkinCompatHelper.INVALID_ID) {
            val textColor = SkinCompatResources.getColor(context, viewTextColorResId)
            this.setTextColor(textColor)
        }
    }

    override fun applySkin() {
        //应用运行间，手动切换换肤回调，再次进行换肤操作
        this.applyTopBarBackgroundColor()
    }

}